package com.yxw.winsdombj;

import android.content.Intent;
import android.media.Image;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.yxw.winsdombj.utils.PrefUtils;

import java.util.ArrayList;
import java.util.List;

public class GuideActivity extends AppCompatActivity {
    private ViewPager mViewPage;

    private int[] mImageIds = new int[]{R.drawable.guide_1, R.drawable.guide_2, R.drawable.guide_3};

    private List<ImageView> mImageViewList;

    private LinearLayout llContainer;

    private ImageView ivRedPoint;

    private int mPointDis;

    //开始体验按钮
    private Button btnStart;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //去掉标题栏，必须在setContentView之前调用
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_guide);

        mViewPage = findViewById(R.id.vp_guide);
        //小圆点
        llContainer = findViewById(R.id.ll_container);
        ivRedPoint = findViewById(R.id.iv_red_point);
//初始化数据
        initData();

        //设置数据
        mViewPage.setAdapter(new GuideAdpater());

        mViewPage.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            /**
             * 某个页面被选中
             * @param position
             * @param positionOffset
             * @param positionOffsetPixels
             */
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                System.out.println("当前位置：" + position + ";移动偏移百分比：" + positionOffset);
//更新小红点距离
                int leftMargin = (int) (mPointDis * positionOffset) +
                        position * mPointDis;//计算小红点当前的左边距
                RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) ivRedPoint.getLayoutParams();

                //修改左边距
                params.leftMargin = leftMargin;
                //重新设置布局参数
                ivRedPoint.setLayoutParams(params);
            }

            /**
             * 当页面滑动过程中的回调
             * @param i
             */
            @Override
            public void onPageSelected(int position) {
                if (position == mImageViewList.size() - 1) {
//最后一个页面显示开始体验的按钮
                    btnStart.setVisibility(View.VISIBLE);

                } else {
                    btnStart.setVisibility(View.INVISIBLE);
                }
            }

            /**
             * 页面状态发生变化的回调
             * @param i
             */
            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });

/*
计算两个圆点的距离
移动距离=第二个远点left值-第一个圆点left值
measure->layput->draw(activity的onCreate方法执行结束之后才会走此流程)
 */
//mPointDirs=llContainer.getChildAt(1).getLeft()-llContainer.getChildAt(0).getLeft();
/*
监听layout方法结束的事件，位置确定之后在获取圆点数据

 */
        ivRedPoint.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                //移除监听，避免重复回调
                ivRedPoint.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                mPointDis = llContainer.getChildAt(1).getLeft() - llContainer.getChildAt(0).getLeft();
                System.out.println("圆点距离：" + mPointDis);
            }
        });


        btnStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //更新sp，已经不是第一次进入了
                PrefUtils.setBoolean(getApplicationContext(),"is_first_enter",false);
                //跳到主页面
                startActivity(new Intent(getApplicationContext(),MainActivity.class));
                finish();
            }
        });

    }

    public void initData() {

        mImageViewList = new ArrayList<ImageView>();
        for (int i = 0; i < mImageIds.length; i++) {
            ImageView view = new ImageView(this);
            //通过设置背景，可以让宽高填充布局
            view.setBackgroundResource(mImageIds[i]);
            mImageViewList.add(view);


            //初始化小圆点
            ImageView point = new ImageView(this);
            //设置图片（shape形状）
            point.setImageResource(R.drawable.shape_point_gray);

            /*
            初始化布局参数，宽高包裹内容，父控件是谁，就是谁声明的布局参数
             */
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            if (i > 0) {
                //从第二个点开始设置左边距
                params.leftMargin = 10;
            }
//设置布局参数
            point.setLayoutParams(params);
            //给容器添加圆点
            llContainer.addView(point);
        }
    }

    class GuideAdpater extends PagerAdapter {
        @Override
        public int getCount() {
            return mImageViewList.size();
        }

        @Override
        public boolean isViewFromObject(@NonNull View view, @NonNull Object o) {
            return view == o;
        }

        /**
         * 初始化item布局
         *
         * @param container
         * @param position
         * @return
         */
        @NonNull
        @Override
        public Object instantiateItem(@NonNull ViewGroup container, int position) {

            ImageView view = mImageViewList.get(position);
            container.addView(view);

            return view;
        }

        /**
         * 销毁item
         *
         * @param container
         * @param position
         * @param object
         */
        @Override
        public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {

            container.removeView((View) object);
        }
    }
}
